package com.shuziren.controller.interciew_transcripts;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.shuziren.abnormal.CustomException;
import com.shuziren.core.vo.Result;
import com.shuziren.pojo.entity.InterviewRecover;
import com.shuziren.pojo.vo.InterviewRecoverQueryVo;
import com.shuziren.service.CandidateService;
import com.shuziren.service.InterviewRecoverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/interviewRecover")
public class InterviewRecoverController {

    @Autowired
    private InterviewRecoverService interviewRecoverService;

    @Autowired
    private CandidateService candidateService;


    /**
     * 保存面试回复对象
     * @param interviewRecover
     * @return
     * @throws CustomException
     */
    @PostMapping("/save")
    public Result save(@Validated @RequestBody InterviewRecover interviewRecover) throws CustomException {
        if (candidateService.getById(interviewRecover.getCandidateId()) == null){
            throw new CustomException("该候选人不存在");
        }

        interviewRecover.setCreatedAt(new Date());
        boolean result = interviewRecoverService.save(interviewRecover);
        if (!result) {
            throw new CustomException("保存面试回复失败。");
        }
        return Result.success(result);
    }

    /**
     * 删除面试回复对象
     * @param queryVo
     * @return
     * @throws CustomException
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody InterviewRecoverQueryVo queryVo) throws CustomException {
        QueryWrapper<InterviewRecover> queryWrapper = new QueryWrapper<>();
        if (queryVo.getCandidateId() != 0 && queryVo.getCandidateId()!= null) {
            queryWrapper.eq("candidate_id", queryVo.getCandidateId());
        }
        if (queryVo.getQuestionId() != 0 && queryVo.getCandidateId()!= null) {
            queryWrapper.eq("question_id", queryVo.getQuestionId());
        }
        // 调用服务层方法按条件删除面试恢复对象
        boolean result = interviewRecoverService.remove(queryWrapper);
        if (!result) {
            throw new CustomException("删除面试回复失败。");
        }
        return Result.success(result);
    }


    /**
     * 根据条件获取面试回复对象列表
     * @param queryVo
     * @return
     * @throws CustomException
     */
    @PostMapping("/list-by-condition")
    public Result listByCondition(@Validated @RequestBody InterviewRecoverQueryVo queryVo) throws CustomException {
        if (candidateService.getById(queryVo.getCandidateId()) == null){
            throw new CustomException("该候选人不存在");
        }
        Page<InterviewRecover> page = new Page<>(queryVo.getPageNum(), queryVo.getPageSize());
        Page<InterviewRecover> recoversPage = interviewRecoverService.selectByConditionWithPage(queryVo, page);
        if (recoversPage == null) {
            throw new CustomException("根据条件获取面试回复列表失败。");
        }
        return Result.success(recoversPage);
    }
}
